package com.api.oa.mapper;

import com.api.oa.model.entity.YQRequest;
import com.api.oa.model.params.YQRequestParams;
import com.api.oa.model.vo.OaNoticeVo;
import com.api.oa.model.vo.YQRequestVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface YQRequestMapper {

    @Insert("insert into  yq_request  (" +
        "orderCode," +
        "requestName," +
        "requestIdCard," +
        "requestIns," +
        "insName," +
        "guardian," +
        "contacts," +
        "patientAddress," +
        "lat," +
        "lng," +
        "conditionDesc," +
        "createdAt," +
        "requestSex," +
        "status," +
        "idCardType," +
        "userId," +
        "insUserId," +
        "ssCard" +
    ") " +
    "values " +
    "(" +
        "#{entity.orderCode}," +
        "#{entity.requestName}," +
        "#{entity.requestIdCard}," +
        "#{entity.requestIns}," +
        "#{entity.insName}," +
        "#{entity.guardian}," +
        "#{entity.contacts}," +
        "#{entity.patientAddress}," +
        "#{entity.lat}," +
        "#{entity.lng}," +
        "#{entity.conditionDesc}," +
        "#{entity.createdAt}," +
        "#{entity.requestSex}," +
        "#{entity.status}," +
        "#{entity.idCardType}," +
        "#{entity.userId}," +
        "#{entity.insUserId}," +
        "#{entity.ssCard}" +
    ")" )
    //@Options(useGeneratedKeys = true, keyProperty = "noticeId", keyColumn = "id")
    Integer insert(@Param("entity") YQRequest entity);



    @Update("update  yq_request  set " +
        "requestName = #{entity.requestName}," +
        "requestIdCard = #{entity.requestIdCard}," +
        "requestIns = #{entity.requestIns}," +
        "insName = #{entity.insName}," +
        "guardian = #{entity.guardian}," +
        "contacts = #{entity.contacts}," +
        "patientAddress = #{entity.patientAddress}," +
        "lat = #{entity.lat}," +
        "lng = #{entity.lng}," +
        "conditionDesc = #{entity.conditionDesc}," +
        "createdAt = #{entity.createdAt}," +
        "requestSex = #{entity.requestSex}," +
        "status = #{entity.status}," +
        "idCardType = #{entity.idCardType}," +
        "insUserId = #{entity.insUserId}," +
        "ssCard = #{entity.ssCard}" +
    " where orderCode = #{entity.orderCode}")
    Integer update(@Param("entity") YQRequest  entity);


    @Delete("delete from yq_request where orderCode=#{id}")
    void delete(@Param("id") String id);


    @Select("<script>select * from  yq_request  <where> 1 = 1 <if test='userId !=null'> and requestIns = #{requestIns} </if> " +
            "<if test='idCard !=null'> requestIdCard like concat('%',#{idCard},'%') </if></where> </script>")
    List<YQRequest> list(@Param("requestIns") String requestIns,@Param("idCard") String idCard);


    @Select("select * from  yq_request  where orderCode = #{orderCode} limit 1")
    YQRequestVO findOne(@Param("orderCode") String orderCode);


    @Select("select * from  yq_request  where requestIdCard = #{requestIdCard} limit 1")
    YQRequestVO findByIdCard(@Param("requestIdCard") String requestIdCard);


    @Select("select count(*) from  yq_request  where  requestIdCard = #{requestIdCard} and status != 3 limit 1")
    Integer isRequesting(@Param("requestIdCard") String requestIdCard);


    /**
     * 身份证搜索  分页 移动端
     * @param idCard
     * @return
     */
    @Select("<script> select yq_request.*,yq_request_ass.userId as assUserId from  yq_request left join yq_request_ass on  yq_request_ass.orderCode = yq_request.orderCode and  yq_request_ass.userRole = 5 <where> " +
            "<if test='idCard !=null'> requestIdCard like concat('%',#{idCard},'%') </if> " +
            "<if test='insUserId !=null'> and insUserId =#{insUserId}</if> " +
            "<if test='status == 0'> and  status = #{status}</if> " +
            "<if test='status !=0 '> and status > 0 </if> " +
            "<if test='assUser!=null'> and yq_request_ass.userId = #{assUser} </if>  " +
            " </where> </script>")
    List<YQRequestVO> queryByIdCardIns(@Param("idCard") String idCard,@Param("insUserId") Long insUserId,@Param("status") Long status,@Param("assUser") Long assUserId);


    /**
     * 身份证搜索  分页 移动端
     * @param idCard
     * @return
     */
    @Select("<script> select yq_request.*,yq_request_ass.userId as assUserId from  yq_request left join yq_request_ass on  yq_request_ass.orderCode = yq_request.orderCode and  yq_request_ass.userRole =  7<where> " +
            " <if test='idCard !=null'> requestIdCard like concat('%',#{idCard},'%') </if> " +
            " <if test='insUserId !=null'> and insUserId =#{insUserId}</if>" +
            " <if test='isAss'> and status &gt; 1 </if> " +
            " <if test='!isAss'> and status = 1 </if> " +
            " <if test='assUser!=null'> and yq_request_ass.userId = #{assUser} </if> </where> </script>")
    List<YQRequestVO> queryByIdCardGov(@Param("idCard") String idCard,@Param("insUserId") Long insUserId,@Param("isAss") Boolean isAss,@Param("assUser") Long assUserId);

    @Update("update  yq_request  set " +
            "status = #{status}" +
            " where orderCode = #{orderCode}")
    Integer updateStatus(@Param("status") Integer status,@Param("orderCode") String orderCode);


    @Select("select * from  yq_request  where  userId = #{userId} and status != 3 limit 1")
    YQRequest findOneByUserId(@Param("userId") Long userId);



    @Update("update  yq_request  set " +
            "lat = #{lat}," +
            "lng = #{lng}" +
            " where orderCode = #{orderCode}")
    Integer updateLocation(@Param("lat")Double lat, @Param("lng")Double lng, @Param("orderCode")String id);


    /**
     * 机构  及机构监管 web
     * @param idCard
     * @param insUserId
     * @param status
     * @param assUserId
     * @return
     */
    @Select("<script> select yq_request.*,yq_request_ass.userId as assUserId from  yq_request left join yq_request_ass on  yq_request_ass.orderCode = yq_request.orderCode and  yq_request_ass.userRole = 5 <where> " +
            "<if test='idCard !=null'> requestIdCard like concat('%',#{idCard},'%') </if> " +
            "<if test='insUserId !=null'> and insUserId =#{insUserId}</if> " +
            "<if test='status !=null'> and  status = #{status} </if> " +
            "<if test='assUser!=null'> and yq_request_ass.userId = #{assUser} </if>  " +
            " </where> </script>")
    List<YQRequestVO> listIns(@Param("idCard") String idCard, @Param("insUserId") Long insUserId, @Param("status") Long status, @Param("assUser") Long assUserId);


    /**
     * 政府 及政府监管 web
     * @param idCard
     * @param insUserId
     * @param status
     * @param assUserId
     * @return
     */
    @Select("<script> select yq_request.*,yq_request_ass.userId as assUserId  from  yq_request left join yq_request_ass on  yq_request_ass.orderCode = yq_request.orderCode and  yq_request_ass.userRole = 7  <where>  " +
            "<if test='idCard !=null'> and requestIdCard like concat('%',#{idCard},'%') </if> " +
            "<if test='insUserId !=null'> and insUserId =#{insUserId}</if> " +
            "<if test='status !=null'> and status = #{status} </if> " +
            "<if test='requestIns !=null'> and requestIns = #{requestIns} </if> " +
            "<if test='assUser!=null'> and yq_request_ass.userId = #{assUser} </if>  " +
            " </where> </script>")
    List<YQRequestVO> listGov(@Param("idCard") String idCard,@Param("insUserId") Long insUserId,@Param("status") Long status,@Param("assUser") Long assUserId,@Param("requestIns") String requestIns);


    @Select("<script> select *  from  yq_request <where>  " +
            "<if test='insCode !=null and insCode!=\"\"'> and requestIns = #{insCode} </if> " +
            " </where> </script>")
    List<YQRequest> requests(@Param("insCode") String insCode);
}

